﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;
namespace Human
{
	/// <summary>
	/// Summary description for frmHocvan.
	/// </summary>
	public class frmHocvan : System.Windows.Forms.Form
	{
        private AccessData mDB = new AccessData();
        Language lan = new Language();
		private DataSet ds=new DataSet();
        int flag = 0; string m_user = "";
		private System.Windows.Forms.DataGrid dgrDanhMuc2;
		private System.Windows.Forms.Panel panel1;
		private PinkieControls.ButtonXP btnSua;
		private PinkieControls.ButtonXP btnBoqua;
		private PinkieControls.ButtonXP btnHuy;
		private PinkieControls.ButtonXP btnThem;
		private PinkieControls.ButtonXP btnLuu;
		private PinkieControls.ButtonXP btnKetthuc;
		private System.Windows.Forms.TextBox txtTen;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtMa;
		private System.Windows.Forms.Label label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmHocvan()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmHocvan));
            this.dgrDanhMuc2 = new System.Windows.Forms.DataGrid();
            this.panel1 = new System.Windows.Forms.Panel();
            this.btnSua = new PinkieControls.ButtonXP();
            this.btnBoqua = new PinkieControls.ButtonXP();
            this.btnHuy = new PinkieControls.ButtonXP();
            this.btnThem = new PinkieControls.ButtonXP();
            this.btnLuu = new PinkieControls.ButtonXP();
            this.btnKetthuc = new PinkieControls.ButtonXP();
            this.txtTen = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtMa = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc2)).BeginInit();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // dgrDanhMuc2
            // 
            this.dgrDanhMuc2.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dgrDanhMuc2.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc2.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dgrDanhMuc2.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dgrDanhMuc2.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
            this.dgrDanhMuc2.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc2.CaptionVisible = false;
            this.dgrDanhMuc2.DataMember = "";
            this.dgrDanhMuc2.FlatMode = true;
            this.dgrDanhMuc2.Font = new System.Drawing.Font("Tahoma", 8F);
            this.dgrDanhMuc2.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc2.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc2.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dgrDanhMuc2.HeaderBackColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc2.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dgrDanhMuc2.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc2.LinkColor = System.Drawing.Color.Teal;
            this.dgrDanhMuc2.Location = new System.Drawing.Point(8, 8);
            this.dgrDanhMuc2.Name = "dgrDanhMuc2";
            this.dgrDanhMuc2.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc2.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc2.RowHeaderWidth = 10;
            this.dgrDanhMuc2.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dgrDanhMuc2.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc2.Size = new System.Drawing.Size(464, 304);
            this.dgrDanhMuc2.TabIndex = 13;
            this.dgrDanhMuc2.CurrentCellChanged += new System.EventHandler(this.dgrDanhMuc2_CurrentCellChanged);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btnSua);
            this.panel1.Controls.Add(this.btnBoqua);
            this.panel1.Controls.Add(this.btnHuy);
            this.panel1.Controls.Add(this.btnThem);
            this.panel1.Controls.Add(this.btnLuu);
            this.panel1.Controls.Add(this.btnKetthuc);
            this.panel1.Location = new System.Drawing.Point(32, 352);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(410, 42);
            this.panel1.TabIndex = 122;
            // 
            // btnSua
            // 
            this.btnSua.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            this.btnSua.DefaultScheme = true;
            this.btnSua.DialogResult = System.Windows.Forms.DialogResult.None;
            this.btnSua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnSua.ForeColor = System.Drawing.Color.DarkBlue;
            this.btnSua.Hint = "";
            this.btnSua.Location = new System.Drawing.Point(67, 4);
            this.btnSua.Name = "btnSua";
            this.btnSua.Scheme = PinkieControls.ButtonXP.Schemes.Blue;
            this.btnSua.Size = new System.Drawing.Size(63, 31);
            this.btnSua.TabIndex = 2;
            this.btnSua.Text = "&Sửa";
            this.btnSua.TextColor = System.Drawing.Color.DarkBlue;
            this.btnSua.Click += new System.EventHandler(this.btnSua_Click);
            // 
            // btnBoqua
            // 
            this.btnBoqua.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            this.btnBoqua.DefaultScheme = true;
            this.btnBoqua.DialogResult = System.Windows.Forms.DialogResult.None;
            this.btnBoqua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnBoqua.ForeColor = System.Drawing.Color.DarkBlue;
            this.btnBoqua.Hint = "";
            this.btnBoqua.Image = ((System.Drawing.Image)(resources.GetObject("btnBoqua.Image")));
            this.btnBoqua.Location = new System.Drawing.Point(195, 4);
            this.btnBoqua.Name = "btnBoqua";
            this.btnBoqua.Scheme = PinkieControls.ButtonXP.Schemes.Blue;
            this.btnBoqua.Size = new System.Drawing.Size(74, 31);
            this.btnBoqua.TabIndex = 4;
            this.btnBoqua.Text = "&Bỏ qua";
            this.btnBoqua.TextColor = System.Drawing.Color.DarkBlue;
            this.btnBoqua.Click += new System.EventHandler(this.btnBoqua_Click);
            // 
            // btnHuy
            // 
            this.btnHuy.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            this.btnHuy.DefaultScheme = true;
            this.btnHuy.DialogResult = System.Windows.Forms.DialogResult.None;
            this.btnHuy.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnHuy.ForeColor = System.Drawing.Color.DarkBlue;
            this.btnHuy.Hint = "";
            this.btnHuy.Image = ((System.Drawing.Image)(resources.GetObject("btnHuy.Image")));
            this.btnHuy.Location = new System.Drawing.Point(270, 4);
            this.btnHuy.Name = "btnHuy";
            this.btnHuy.Scheme = PinkieControls.ButtonXP.Schemes.Blue;
            this.btnHuy.Size = new System.Drawing.Size(61, 31);
            this.btnHuy.TabIndex = 5;
            this.btnHuy.Text = "&Xoá ";
            this.btnHuy.TextColor = System.Drawing.Color.DarkBlue;
            this.btnHuy.Click += new System.EventHandler(this.btnHuy_Click);
            // 
            // btnThem
            // 
            this.btnThem.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            this.btnThem.DefaultScheme = true;
            this.btnThem.DialogResult = System.Windows.Forms.DialogResult.None;
            this.btnThem.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnThem.ForeColor = System.Drawing.Color.DarkBlue;
            this.btnThem.Hint = "";
            this.btnThem.Image = ((System.Drawing.Image)(resources.GetObject("btnThem.Image")));
            this.btnThem.Location = new System.Drawing.Point(5, 4);
            this.btnThem.Name = "btnThem";
            this.btnThem.Scheme = PinkieControls.ButtonXP.Schemes.Blue;
            this.btnThem.Size = new System.Drawing.Size(63, 31);
            this.btnThem.TabIndex = 1;
            this.btnThem.Text = "&Mới";
            this.btnThem.TextColor = System.Drawing.Color.DarkBlue;
            this.btnThem.Click += new System.EventHandler(this.btnThem_Click);
            // 
            // btnLuu
            // 
            this.btnLuu.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            this.btnLuu.DefaultScheme = true;
            this.btnLuu.DialogResult = System.Windows.Forms.DialogResult.None;
            this.btnLuu.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnLuu.ForeColor = System.Drawing.Color.DarkBlue;
            this.btnLuu.Hint = "";
            this.btnLuu.Image = ((System.Drawing.Image)(resources.GetObject("btnLuu.Image")));
            this.btnLuu.Location = new System.Drawing.Point(131, 4);
            this.btnLuu.Name = "btnLuu";
            this.btnLuu.Scheme = PinkieControls.ButtonXP.Schemes.Blue;
            this.btnLuu.Size = new System.Drawing.Size(63, 31);
            this.btnLuu.TabIndex = 3;
            this.btnLuu.Text = "&Lưu";
            this.btnLuu.TextColor = System.Drawing.Color.DarkBlue;
            this.btnLuu.Click += new System.EventHandler(this.btnLuu_Click);
            // 
            // btnKetthuc
            // 
            this.btnKetthuc.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            this.btnKetthuc.DefaultScheme = true;
            this.btnKetthuc.DialogResult = System.Windows.Forms.DialogResult.None;
            this.btnKetthuc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnKetthuc.ForeColor = System.Drawing.Color.DarkBlue;
            this.btnKetthuc.Hint = "";
            this.btnKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("btnKetthuc.Image")));
            this.btnKetthuc.Location = new System.Drawing.Point(332, 4);
            this.btnKetthuc.Name = "btnKetthuc";
            this.btnKetthuc.Scheme = PinkieControls.ButtonXP.Schemes.Blue;
            this.btnKetthuc.Size = new System.Drawing.Size(71, 31);
            this.btnKetthuc.TabIndex = 6;
            this.btnKetthuc.Text = "&Thoát";
            this.btnKetthuc.TextColor = System.Drawing.Color.DarkBlue;
            this.btnKetthuc.Click += new System.EventHandler(this.btnKetthuc_Click);
            // 
            // txtTen
            // 
            this.txtTen.BackColor = System.Drawing.Color.White;
            this.txtTen.ForeColor = System.Drawing.Color.DarkBlue;
            this.txtTen.Location = new System.Drawing.Point(248, 318);
            this.txtTen.MaxLength = 40;
            this.txtTen.Name = "txtTen";
            this.txtTen.Size = new System.Drawing.Size(216, 20);
            this.txtTen.TabIndex = 121;
            this.txtTen.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtTen_KeyDown);
            // 
            // label2
            // 
            this.label2.BackColor = System.Drawing.SystemColors.Control;
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(168, 328);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(74, 13);
            this.label2.TabIndex = 120;
            this.label2.Text = "Tên học vấn :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtMa
            // 
            this.txtMa.BackColor = System.Drawing.Color.White;
            this.txtMa.Enabled = false;
            this.txtMa.ForeColor = System.Drawing.Color.DarkBlue;
            this.txtMa.Location = new System.Drawing.Point(88, 320);
            this.txtMa.Name = "txtMa";
            this.txtMa.Size = new System.Drawing.Size(64, 20);
            this.txtMa.TabIndex = 119;
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.SystemColors.Control;
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(16, 328);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(70, 13);
            this.label1.TabIndex = 118;
            this.label1.Text = "Mã học vấn :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // frmHocvan
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(480, 400);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.txtTen);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtMa);
            this.Controls.Add(this.dgrDanhMuc2);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmHocvan";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh mục học vấn";
            this.Load += new System.EventHandler(this.frmHocvan_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc2)).EndInit();
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmHocvan_Load(object sender, System.EventArgs e)
		{
			hocvan();
			Ena_control(true);
			refresh_Text();
			Ena_textbox(false);
			btnThem.Focus();
		}
		private void hocvan() 	
		{
			DataSet ds1=new DataSet();
			ds1=mDB.get_data("SELECT mahv,tenhv,mabc FROM "+mDB.user+".hocvan order by mahv");
			dgrDanhMuc2.DataSource=null;
			dgrDanhMuc2.TableStyles.Clear();
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = ds1.Tables[0].TableName;
		
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			
			DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "mahv";
			TextCol1.HeaderText = lan.Change_language_MessageText("Mã học vấn");
			TextCol1.Width = 120;
			TextCol1.ReadOnly=true;
			TextCol1.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol1);
			dgrDanhMuc2.TableStyles.Add(ts);
		
			DataGridTextBoxColumn TextCol2=new DataGridTextBoxColumn();
			TextCol2.MappingName = "tenhv";
			TextCol2.HeaderText = lan.Change_language_MessageText("Tên học vấn");
			TextCol2.Width = 290;
			TextCol2.ReadOnly=true;
			TextCol2.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol2);
			dgrDanhMuc2.TableStyles.Add(ts);		
		 			 
			ts.DataGrid.DataSource=ds1.Tables[0];
            CurrencyManager cm = (CurrencyManager)BindingContext[dgrDanhMuc2.DataSource, dgrDanhMuc2.DataMember];
            DataView dv = (DataView)cm.List;
            dv.AllowDelete = false;
            dv.AllowEdit = false;
            dv.AllowNew = false;    
		}
        private string get_maxid()
        {
            int m_tam = 0;
            ds = mDB.get_data("SELECT Max(to_number(mahv,9999)) FROM " + mDB.user + ".hocvan");
            if (ds.Tables[0].Rows.Count > 0)
                m_tam = ds.Tables[0].Rows[0][0].ToString() !="" ? int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1:1;	
            else
                m_tam = 1;
            return m_tam.ToString().PadLeft(4, '0').ToString();	
        }
		private void btnThem_Click(object sender, System.EventArgs e)
		{
			Ena_control(false);
			Ena_textbox(true);
			clear_Text();
            m_user = mDB.user;
            txtMa.Text = get_maxid();
            flag = 0;			
			txtTen.Focus();
		}

		private void btnSua_Click(object sender, System.EventArgs e)
		{
            flag = 1;
            Ena_control(false);
			Ena_textbox(false);		
			txtTen.Enabled=true;
			txtTen.Focus();
		}

		private void btnBoqua_Click(object sender, System.EventArgs e)
		{
			Ena_control(true);
			txtTen.Enabled=false;
			refresh_Text();
			btnKetthuc.Focus();
		}
		private void Ena_control(bool ena)
		{
			btnThem.Enabled=ena;
			btnSua.Enabled=ena;
			btnLuu.Enabled=!ena;
			btnBoqua.Enabled=!ena;
			btnHuy.Enabled=ena;
			btnKetthuc.Enabled=ena;
		}
		private void Ena_textbox(bool ena)
		{			
			txtTen.Enabled=ena;
		}
		private void clear_Text()
		{			
			txtTen.Clear();
		}
		private void refresh_Text()
		{
			int i=dgrDanhMuc2.CurrentRowIndex;
			if(i>=0)
			{
				txtMa.Text=dgrDanhMuc2[i,0].ToString();
				txtTen.Text=dgrDanhMuc2[i,1].ToString();
			}
		}
		private void btnKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnHuy_Click(object sender, System.EventArgs e)
		{
            int i = dgrDanhMuc2.CurrentCell.RowNumber;
			if(i<0)  
				i=dgrDanhMuc2.CurrentCell.RowNumber;
			if(i==dgrDanhMuc2.CurrentCell.RowNumber) 
			{								
				DialogResult dr = MessageBox.Show(lan.Change_language_MessageText("Đồng ý huỷ thông tin này?"),AccessData.Msg,MessageBoxButtons.YesNo);			
				if(dr == DialogResult.Yes ) 			
				{
                    if (!kemtra(txtMa.Text.Trim()))
                    {
                        mDB.get_data("DELETE  FROM " + mDB.user + ".hocvan WHERE mahv='" + txtMa.Text.Trim() + "'");
                    }
				}
                hocvan();
			}
		}

		private void btnLuu_Click(object sender, System.EventArgs e)
		{
            int m_id = 0;
			try
			{
                if (flag == 0)
                {
                    m_id = Int32.Parse(get_maxid());
                    txtMa.Text = m_id.ToString().PadLeft(3, '0');
                }
                else
                {
                    if (txtMa.Text != "")
                        m_id = Int32.Parse(txtMa.Text.ToString());
                }
                mDB.upd_DMHOCVAN(txtMa.Text,txtTen.Text);
				hocvan();
                flag = 0;
				Ena_control(true);				
				Ena_textbox(false);
				refresh_Text();
			}
			catch{}
		}

		private void dgrDanhMuc2_CurrentCellChanged(object sender, System.EventArgs e)
		{
			try
			{
				refresh_Text();
				Ena_textbox(false);
				Ena_control(true);
			}
			catch{}
		}

		private void txtTen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			
            if (e.KeyCode == Keys.Enter)
            {
                btnLuu.Focus();
            }
		}

        private bool kemtra(string mahv)
        {
            bool flag = false;
            try
            {
                flag = mDB.get_data("select count(*) as tong from " + mDB.user + ".llnv where mahv='" + mahv + "'").Tables[0].Rows[0][0].ToString() !="0";
            }
            catch
            {
                flag = false;
            }
            if (flag)
                MessageBox.Show(lan.Change_language_MessageText("Học vấn đã sử dụng không thể hủy!"),AccessData.Msg);
            return flag;
        }
	}
}
